#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<unordered_map>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
struct P{
    LL l,r;
}a[maxn];
unordered_map<LL,LL>map1;
bool cmp(P A,P B){
     if(A.r==B.r) return A.l<B.l;
     return A.r<B.r;
}
int main(void){
   	cin.tie(0);std::ios::sync_with_stdio(false);
    LL n;cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i].l>>a[i].r;
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++) map1[a[i].r]=max(map1[a[i].r],a[i].l);
    LL mx=-1;LL ans=0;
    for(int i=1;i<=n;i++){
        if(mx==-1) mx=map1[a[i].r];
        if(mx==map1[a[i].r]){
            ans+=mx;
        }
        else if(mx!=map1[a[i].r]){
            if(mx>=map1[a[i].r]){
                map1[a[i].r]=mx;
                ans+=mx;
            }
            else{
                mx=map1[a[i].r];
                ans+=mx;
            }
        }
    }
    cout<<ans<<endl;
   	return 0;
}
